﻿using LayeredSkin;
using LayeredSkin.Controls;
using LayeredSkin.DirectUI;
using LayeredSkin.Forms;
using LitJson;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Top_Meeting.UI;
using Top_Meeting.Window;

namespace Top_Meeting
{
    static class Helper
    {

        #region PRIVATE_METHOD

        #region Custom_Align      


        #region Control
        private static Control AlignLeft(this Control _ctl, AnchorStyles _anchorStyles = AnchorStyles.Left)
        {
            _ctl.Anchor = _anchorStyles;
            if (_ctl.Parent != null)
            {
                _ctl.Left = 0 + _ctl.Parent.Padding.Left;
            }
            else
            {
                _ctl.Left = 0;

            }
            return _ctl;
        }

        private static Control AlignTop(this Control _ctl, AnchorStyles _anchorStyles = AnchorStyles.Top)
        {
            _ctl.Anchor = _anchorStyles;
            if (_ctl.Parent != null)
            {
                _ctl.Top = 0 + _ctl.Parent.Padding.Top;
            }
            else
            {
                _ctl.Top = 0;

            }
            return _ctl;
        }

        private static Control AlignRight(this Control _ctl, AnchorStyles _anchorStyles = AnchorStyles.Right)
        {
            _ctl.Anchor = _anchorStyles;
            if (_ctl.Parent != null)
            {
                _ctl.Left = _ctl.Parent.Width - _ctl.Width + _ctl.Parent.Padding.Right;
            }
            else
            {
                _ctl.Left = Screen.PrimaryScreen.WorkingArea.Width - _ctl.Width;
            }

            return _ctl;
        }

        private static Control AlignBottom(this Control _ctl, AnchorStyles _anchorStyles = AnchorStyles.Bottom)
        {
            _ctl.Anchor = _anchorStyles;
            if (_ctl.Parent != null)
            {
                _ctl.Top = _ctl.Parent.Height - _ctl.Height + _ctl.Parent.Padding.Bottom;
            }
            else
            {
                _ctl.Top = Screen.PrimaryScreen.WorkingArea.Height - _ctl.Height;
            }

            return _ctl;
        }

        private static Control AlignCenter(this Control _ctl, AnchorStyles _anchorStyles = AnchorStyles.None)
        {
            _ctl.Anchor = _anchorStyles;
            if (_ctl.Parent != null)
            {
                _ctl.Left = (_ctl.Parent.Width + (_ctl.Padding.Left - _ctl.Parent.Padding.Right) - _ctl.Width) / 2;
                _ctl.Top = (_ctl.Parent.Height + (_ctl.Padding.Top - _ctl.Parent.Padding.Bottom) - _ctl.Height) / 2;
            }
            else
            {
                _ctl.Left = (Screen.PrimaryScreen.WorkingArea.Width - _ctl.Width) / 2;
                _ctl.Top = (Screen.PrimaryScreen.WorkingArea.Height - _ctl.Height) / 2;
            }

            return _ctl;
        }

        private static Control AlignHorizontal(this Control _ctl, AnchorStyles _anchorStyles = AnchorStyles.None)
        {
            _ctl.Anchor = _anchorStyles;
            if (_ctl.Parent != null)
            {
                _ctl.Left = (_ctl.Parent.Width + (_ctl.Padding.Left - _ctl.Parent.Padding.Right) - _ctl.Width) / 2;
            }
            else
            {
                _ctl.Left = (Screen.PrimaryScreen.WorkingArea.Width - _ctl.Width) / 2;
            }

            return _ctl;
        }

        private static Control AlignVertical(this Control _ctl, AnchorStyles _anchorStyles = AnchorStyles.None)
        {
            _ctl.Anchor = _anchorStyles;
            if (_ctl.Parent != null)
            {
                _ctl.Top = (_ctl.Parent.Height + (_ctl.Padding.Top - _ctl.Parent.Padding.Bottom) - _ctl.Height) / 2;
            }
            else
            {
                _ctl.Top = (Screen.PrimaryScreen.WorkingArea.Height - _ctl.Height) / 2;
            }

            return _ctl;
        }

        public static Control AlignControl(this Control _ctl, AlignType _alignType, AnchorStyles _anchorStyles, Point _offset)
        {
            if ((_alignType & AlignType.Left) != 0)
            {
                _ctl.AlignLeft();
            }
            if ((_alignType & AlignType.Top) != 0)
            {
                _ctl.AlignTop();
            }
            if ((_alignType & AlignType.Right) != 0)
            {
                _ctl.AlignRight();
            }
            if ((_alignType & AlignType.Bottom) != 0)
            {
                _ctl.AlignBottom();
            }
            if ((_alignType & AlignType.LeftTop) != 0)
            {
                _ctl.AlignLeft();
                _ctl.AlignTop();
            }
            if ((_alignType & AlignType.RightTop) != 0)
            {
                _ctl.AlignRight();
                _ctl.AlignTop();
            }
            if ((_alignType & AlignType.LeftBottom) != 0)
            {
                _ctl.AlignLeft();
                _ctl.AlignBottom();
            }
            if ((_alignType & AlignType.RightBottom) != 0)
            {
                _ctl.AlignRight();
                _ctl.AlignBottom();
            }
            if ((_alignType & AlignType.Horizontal) != 0)
            {
                _ctl.AlignHorizontal();
            }
            if ((_alignType & AlignType.Vertical) != 0)
            {
                _ctl.AlignVertical();
            }
            if ((_alignType & AlignType.Center) != 0)
            {
                _ctl.AlignCenter();
            }

            _ctl.Left += _offset.X;
            _ctl.Top += _offset.Y;

            return _ctl;
        }

        #endregion


        #region DuibaseControl

        private static DuiBaseControl AlignLeft(this DuiBaseControl _ctl)
        {
            var _parent = _ctl.Parent as Control;
            var _duiParent = _ctl.Parent as DuiBaseControl;

            if (_parent != null)
            {
                _ctl.Left = _parent.Padding.Left;
            }
            else if (_duiParent != null)
            {
                _ctl.Left = _duiParent.Margin.Left;
            }
            else
            {
                _ctl.Left = 0;
            }
            return _ctl;
        }

        private static DuiBaseControl AlignTop(this DuiBaseControl _ctl)
        {
            var _parent = _ctl.Parent as Control;
            var _duiParent = _ctl.Parent as DuiBaseControl;

            if (_parent != null)
            {
                _ctl.Left = _parent.Padding.Top;
            }
            else if (_duiParent != null)
            {
                _ctl.Left = _duiParent.Margin.Left;
            }
            else
            {
                _ctl.Left = 0;
            }
            return _ctl;
        }

        private static DuiBaseControl AlignRight(this DuiBaseControl _ctl)
        {
            var _parent = _ctl.Parent as Control;
            var _duiParent = _ctl.Parent as DuiBaseControl;

            if (_parent != null)
            {
                _ctl.Left = _parent.Width - _ctl.Width - _parent.Padding.Right;
            }
            else if (_duiParent != null)
            {
                _ctl.Left = _duiParent.Width - _ctl.Width - _duiParent.Margin.Right;
            }
            else
            {
                _ctl.Left = Screen.PrimaryScreen.WorkingArea.Width - _ctl.Width;
            }

            return _ctl;
        }

        private static DuiBaseControl AlignBottom(this DuiBaseControl _ctl)
        {
            var _parent = _ctl.Parent as Control;
            var _duiParent = _ctl.Parent as DuiBaseControl;

            if (_parent != null)
            {
                _ctl.Top = _parent.Height - _ctl.Height - _parent.Padding.Bottom;
            }
            else if (_duiParent != null)
            {
                _ctl.Left = _duiParent.Height - _ctl.Height - _duiParent.Margin.Bottom;
            }
            else
            {
                _ctl.Top = Screen.PrimaryScreen.WorkingArea.Height - _ctl.Height;
            }

            return _ctl;
        }

        private static DuiBaseControl AlignCenter(this DuiBaseControl _ctl)
        {
            var _parent = _ctl.Parent as Control;
            var _duiParent = _ctl.Parent as DuiBaseControl;

            if (_parent != null)
            {
                _ctl.Left = (_parent.Width + (_parent.Padding.Left - _parent.Padding.Right) - _ctl.Width) / 2;
                _ctl.Top = (_parent.Height + (_parent.Padding.Top - _parent.Padding.Bottom) - _ctl.Height) / 2;
            }
            else if (_duiParent != null)
            {
                _ctl.Left = (_duiParent.Width + (_duiParent.Margin.Left - _duiParent.Margin.Right) - _ctl.Width) / 2;
                _ctl.Top = (_duiParent.Height + (_duiParent.Margin.Top - _duiParent.Margin.Bottom) - _ctl.Height) / 2;
            }
            else
            {
                _ctl.Left = (Screen.PrimaryScreen.WorkingArea.Width - _ctl.Width) / 2;
                _ctl.Top = (Screen.PrimaryScreen.WorkingArea.Height - _ctl.Height) / 2;
            }

            return _ctl;
        }

        private static DuiBaseControl AlignHorizontal(this DuiBaseControl _ctl)
        {
            var _parent = _ctl.Parent as Control;
            var _duiParent = _ctl.Parent as DuiBaseControl;

            if (_parent != null)
            {
                _ctl.Left = (_parent.Width + (_parent.Padding.Left - _parent.Padding.Right) - _ctl.Width) / 2;
            }
            else if (_duiParent != null)
            {
                _ctl.Left = (_duiParent.Width + (_duiParent.Margin.Left - _duiParent.Margin.Right) - _ctl.Width) / 2;
            }
            else
            {
                _ctl.Left = (Screen.PrimaryScreen.WorkingArea.Width - _ctl.Width) / 2;
            }

            return _ctl;
        }

        private static DuiBaseControl AlignVertical(this DuiBaseControl _ctl)
        {
            var _parent = _ctl.Parent as Control;
            var _duiParent = _ctl.Parent as DuiBaseControl;

            if (_parent != null)
            {
                _ctl.Top = (_parent.Height + (_parent.Padding.Top - _parent.Padding.Bottom) - _ctl.Height) / 2;
            }
            else if (_duiParent != null)
            {
                _ctl.Top = (_duiParent.Height + (_duiParent.Margin.Top - _duiParent.Margin.Bottom) - _ctl.Height) / 2;
            }
            else
            {
                _ctl.Top = (Screen.PrimaryScreen.WorkingArea.Height - _ctl.Height) / 2;
            }

            return _ctl;
        }

        public static DuiBaseControl AlignControl(this DuiBaseControl _ctl, AlignType _alignType, Point _offset)
        {
            if ((_alignType & AlignType.Left) != 0)
            {
                _ctl.AlignLeft();
            }
            if ((_alignType & AlignType.Top) != 0)
            {
                _ctl.AlignTop();
            }
            if ((_alignType & AlignType.Right) != 0)
            {
                _ctl.AlignRight();
            }
            if ((_alignType & AlignType.Bottom) != 0)
            {
                _ctl.AlignBottom();
            }
            if ((_alignType & AlignType.LeftTop) != 0)
            {
                _ctl.AlignLeft();
                _ctl.AlignTop();
            }
            if ((_alignType & AlignType.RightTop) != 0)
            {
                _ctl.AlignRight();
                _ctl.AlignTop();
            }
            if ((_alignType & AlignType.LeftBottom) != 0)
            {
                _ctl.AlignLeft();
                _ctl.AlignBottom();
            }
            if ((_alignType & AlignType.RightBottom) != 0)
            {
                _ctl.AlignRight();
                _ctl.AlignBottom();
            }
            if ((_alignType & AlignType.Horizontal) != 0)
            {
                _ctl.AlignHorizontal();
            }
            if ((_alignType & AlignType.Vertical) != 0)
            {
                _ctl.AlignVertical();
            }
            if ((_alignType & AlignType.Center) != 0)
            {
                _ctl.AlignCenter();
            }

            _ctl.Left += _offset.X;
            _ctl.Top += _offset.Y;

            return _ctl;
        }

        #endregion


        #endregion

        #endregion


        #region Skin

        public static Dictionary<string, Image> ReadSkinResources(string path)
        {
            FileStream fs = File.OpenRead(path);
            byte[] headerLength = new byte[4];
            fs.Read(headerLength, 0, headerLength.Length);
            int length = BitConverter.ToInt32(headerLength, 0);

            byte[] headerBuffer = new byte[length];
            fs.Read(headerBuffer, 0, headerBuffer.Length);

            var jsonData = JsonMapper.ToObject(Encoding.UTF8.GetString(headerBuffer));

            List<SkinFile> fileinfoList = new List<SkinFile>();

            foreach (JsonData item in jsonData)
            {
                fileinfoList.Add(new SkinFile(item["FileName"].ToString(), int.Parse((item["FileSize"].ToString()))));
            }

            Dictionary<string, Image> skinResources = new Dictionary<string, Image>();

            MemoryStream ms;

            fileinfoList.ForEach(o =>
            {
                byte[] imgBuffer = new byte[o.FileSize];
                fs.Read(imgBuffer, 0, o.FileSize);
                try
                {
                    ms = new MemoryStream(imgBuffer);
                    skinResources[o.FileName] = Image.FromStream(ms);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("error:" + o.FileName + ex.Message);
                }
            });

            fs.Close();

            return skinResources;
        }

        public static void ColorMouseEnter(object sender, EventArgs e)
        {
            var o = sender as DuiBaseControl;
            if (o != null)
            {
                o.ColorMouseEnter(TMColor.Color_Transparent, TMColor.Color_39f3ff, TMColor.Color_Transparent);
            }
        }

        public static void ColorMouseLeave(object sender, EventArgs e)
        {
            var o = sender as DuiBaseControl;
            if (o != null)
            {
                o.ColorMouseEnter(TMColor.Color_Transparent, Color.White, TMColor.Color_Transparent);
            }
        }

        public static void ColorMouseEnter(this DuiBaseControl _dbc, Color _color, Color _fcolor, Color _bcolor, int _bwidth = 1)
        {
            _dbc.MouseEnter += (o, e) => { _dbc.BackColor = _color; _dbc.ForeColor = _fcolor; };
            _dbc._Borders(_bcolor, _bwidth);
        }

        public static void ColorMouseLeave(this DuiBaseControl _dbc, Color _color, Color _fcolor, Color _bcolor, int _bwidth = 1)
        {
            _dbc.MouseLeave += (o, e) => { _dbc.BackColor = _color; _dbc.ForeColor = _fcolor; };
            _dbc._Borders(_bcolor, _bwidth);
        }

        public static void ColorMouseEnter(this Control _ctl, Color _color, Color _fcolor)
        {
            _ctl.MouseEnter += (o, e) => { _ctl.BackColor = _color; };
        }

        public static void ColorMouseLeave(this Control _ctl, Color _color, Color _fcolor)
        {
            _ctl.MouseLeave += (o, e) => { _ctl.BackColor = _color; };
        }

        public static void _Borders(this DuiBaseControl _dbc, Color _color, int _width)
        {
            _dbc.BordersLeft(_color, _width);
            _dbc.BordersTop(_color, _width);
            _dbc.BordersRight(_color, _width);
            _dbc.BordersBottom(_color, _width);
        }

        public static void BordersLeft(this DuiBaseControl _dbc, Color _color, int _width)
        {
            _dbc.Borders.LeftColor = _color;
            _dbc.Borders.LeftWidth = _width;
        }
        public static void BordersTop(this DuiBaseControl _dbc, Color _color, int _width)
        {
            _dbc.Borders.TopColor = _color;
            _dbc.Borders.TopWidth = _width;
        }
        public static void BordersRight(this DuiBaseControl _dbc, Color _color, int _width)
        {
            _dbc.Borders.RightColor = _color;
            _dbc.Borders.RightWidth = _width;
        }
        public static void BordersBottom(this DuiBaseControl _dbc, Color _color, int _width)
        {
            _dbc.Borders.BottomColor = _color;
            _dbc.Borders.BottomWidth = _width;
        }

        #endregion


        #region Font

        public static PrivateFontCollection LoadFont()
        {
            PrivateFontCollection pfc = new PrivateFontCollection();
            string[] filesPath = Directory.GetFiles(Configuration.FontsPath);
            if (filesPath.Length > 0)
            {
                for (int i = 0; i < filesPath.Length; i++)
                {
                    pfc.AddFontFile(filesPath[i]);
                }
            }

            return pfc;
        }

        public static void SetFont(Control ctl, Font _font, bool withChild = true)
        {
            ctl.Font = _font;

            if (withChild)
            {
                if (ctl.Controls.Count > 0)
                {
                    foreach (Control item in ctl.Controls)
                    {
                        SetFont(item, _font);
                    }
                }
            }
        }

        #endregion


        #region Code

        public static string RefreshCode(Control imgContainer = null)
        {
            var code = CheckCode();
            if (imgContainer != null)
                CodeImage(code, imgContainer);
            return code;
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        public static string CheckCode()        //此方法生成  
        {
            int number;
            char code;
            string checkCode = String.Empty;     //声明变量存储随机生成的4位英文或数字  
            Random random = new Random();        //生成随机数  
            for (int i = 0; i < 4; i++)
            {
                number = random.Next();          //返回非负随机数  
                if (number % 2 == 0)             //判断数字是否为偶数  
                    code = (char)('0' + (char)(number % 10));
                else           //如果不是偶数  
                    code = (char)('A' + (char)(number % 26));
                checkCode += " " + code.ToString();    //累加字符串  
            }
            return checkCode;                    //返回生成的字符串  
        }
        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="checkCode"></param>
        /// <param name="picCode"></param>
        public static void CodeImage(string checkCode, Control picCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;
            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 10.0)), picCode.Height);
            Graphics g = Graphics.FromImage(image);     //创建Graphics对象  
            try
            {
                Random random = new Random();      //生成随机生成器  
                g.Clear(Color.Transparent);         //清空图片背景色  
                for (int i = 0; i < 3; i++)       //画图片的背景噪音线  
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.FromArgb(random.Next(0, 2) * 255, random.Next(0, 2) * 255, random.Next(0, 2) * 255)), x1, y1, x2, y2);
                }
                Font font = new System.Drawing.Font("微软雅黑", 12, (System.Drawing.FontStyle.Bold));

                char[] cs = checkCode.ToCharArray();

                for (int i = 0; i < cs.Length; i++)
                {
                    g.DrawString(cs[i].ToString(), font, new SolidBrush(Configuration.Color_DefultFore), i * 7, random.Next(3) * 5);
                }

                for (int i = 0; i < 150; i++)      //画图片的前景噪音点  
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线  
                // g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                picCode.Width = image.Width;    //设置PictureBox的宽度  
                picCode.Height = image.Height;    //设置PictureBox的高度  
                picCode.BackgroundImage = image;   //设置PictureBox的背景图像  
            }
            catch (Exception ex)
            {
                //LogWriter.Write(LOG_CATEGORY.WIN_UI, LOG_LEVEL.ERROR, String.Format("{0}", ex));
            }
        }

        #endregion


        #region CONTROL

        //public static void MouseDown(this Frm_Base frm, Control ctl, bool withChild = true)
        //{
        //    ctl.MouseDown += frm.Frm_MouseDown;
        //    if (withChild)
        //    {
        //        foreach (Control item in ctl.Controls)
        //        {

        //        }
        //    }
        //}

        public static void _MouseDown(this Control ctl, MouseEventHandler mouseDown)
        {
            ctl.MouseDown += mouseDown;
            foreach (Control item in ctl.Controls)
            {
                _MouseDown(item, mouseDown);
            }
        }

        private static T ShowWindow<T>(Control owner, bool showon, bool isDialog) where T : Form, new()
        {
            var frm = new T();
            if (showon)
            {
                if (isDialog)
                {
                    if (owner != null)
                        frm.ShowDialog(owner);
                    else
                        frm.ShowDialog();
                }
                else
                {
                    if (owner != null)
                        frm.Show(owner);
                    else
                        frm.Show();
                }
            }

            return frm;
        }

        public static T ShowWindow<T>(bool showon = true) where T : Form, new()
        {
            return ShowWindow<T>(null, showon, false);
        }

        public static T ShowWindow<T>(Control owner, bool showon = true) where T : Form, new()
        {
            return ShowWindow<T>(owner, showon, false);
        }

        public static T ShowDialogWindow<T>(bool showon = true) where T : Form, new()
        {
            return ShowWindow<T>(null, showon, true);
        }

        public static T ShowDialogWindow<T>(Control owner, bool showon = true) where T : Form, new()
        {
            return ShowWindow<T>(owner, showon, true);
        }


        //private static T ShowWindow<T>(Control owner, bool showon, bool isDialog) where T : Form, new()
        //{
        //    var frm = new T();
        //    if (showon)
        //    {
        //        if (isDialog)
        //        {
        //            if (owner != null)
        //                frm.ShowDialog(owner);
        //            else
        //                frm.ShowDialog();
        //        }
        //        else
        //        {
        //            if (owner != null)
        //                frm.Show(owner);
        //            else
        //                frm.Show();
        //        }
        //    }

        //    return frm;
        //}

        //public static T ShowWindow<T>(bool showon = true) where T : Form, new()
        //{
        //    return ShowWindow<T>(null, showon, false);
        //}

        //public static T ShowWindow<T>(Control owner, bool showon = true) where T : Form, new()
        //{
        //    return ShowWindow<T>(owner, showon, false);
        //}

        //public static T ShowDialogWindow<T>(bool showon = true) where T : Form, new()
        //{
        //    return ShowWindow<T>(null, showon, true);
        //}

        //public static T ShowDialogWindow<T>(Control owner, bool showon = true) where T : Form, new()
        //{
        //    return ShowWindow<T>(owner, showon, true);
        //}





        public static void Align(this Control ctl, int at)
        {
            if (ctl.Parent != null)
            {
                Control parent = ctl.Parent;

                int left = (at == 0 | at == 1) ? (parent.Width - ctl.Width) / 2 : ctl.Left;
                int top = (at == 0 | at == 2) ? (parent.Height - ctl.Height) / 2 : ctl.Top;
                ctl.Left = left;
                ctl.Top = top;
            }
        }

        public static void Align(this Control ctl, int at, Point offset)
        {
            ctl.Align(at);
            ctl.Location = new Point(ctl.Location.X + offset.X, ctl.Location.Y + offset.Y);
        }

        public static void Align(this Control ctl, Control target, int at)
        {
            if (target != null)
            {
                int left = (at == 0 | at == 1) ? ((target.Width - ctl.Width) / 2) + target.Left : ctl.Left;
                int top = (at == 0 | at == 2) ? ((target.Height - ctl.Height) / 2) + target.Top : ctl.Top;
                ctl.Left = left;
                ctl.Top = top;
            }
        }

        public static void Relatively(this Control ctl, Control target, Point offset, int dis)
        {
            if (target != null)
            {
                Point newPoint = Point.Empty;

                switch (dis)
                {
                    case 0:
                        newPoint = new Point(target.Location.X - ctl.Width - offset.X, offset.Y);
                        break;
                    case 1:
                        newPoint = new Point(offset.X, target.Location.Y - ctl.Height - offset.Y);
                        break;
                    case 2:
                        newPoint = new Point(target.Width + target.Location.X + offset.X, offset.Y);
                        break;
                    case 3:
                        newPoint = new Point(offset.X, target.Height + target.Location.Y + offset.Y);
                        break;
                    default:
                        break;
                }
                ctl.Location = newPoint;
            }
        }

        public static void Align(int at, params Control[] _ctls)
        {
            for (int i = 0; i < _ctls.Length; i++)
            {
                _ctls[i].Align(at);
            }
        }

        public static void ToSize(Size size, params Control[] _ctls)
        {
            for (int i = 0; i < _ctls.Length; i++)
            {
                _ctls[i].Size = size;
            }
        }

        public static void SizeToBG(this Control _ctl)
        {
            var bg = _ctl.BackgroundImage;
            if (bg != null)
            {
                _ctl.Size = bg.Size;
            }
        }

        public static void SizeToBG(params Control[] _ctls)
        {
            for (int i = 0; i < _ctls.Length; i++)
            {
                _ctls[i].SizeToBG();
            }
        }

        public static void SizeToChild(this Control _ctl, int at)
        {
            if (_ctl.HasChildren)
            {
                int _width = (at == 0 | at == 1) ? 0 : _ctl.Width;
                int _height = (at == 0 | at == 2) ? 0 : _ctl.Height;
                foreach (Control item in _ctl.Controls)
                {
                    if (at == 0 | at == 1)
                        _width = _width < item.Width ? item.Width : _width;
                    if (at == 0 | at == 2)
                        _height = _height < item.Height ? item.Height : _height;
                }
                _ctl.Width = _width;
                _ctl.Height = _height;
            }
        }

        public static void ImageToBackground(this LayeredPanel _ctl, string imgName = "")
        {
            var _name = imgName == String.Empty ? _ctl.Name : imgName;

            var tmpImg = TMUI.SkinImage(_name);
            if (tmpImg != null)
            {
                _ctl.BackgroundImage = tmpImg;
            }

            var _cname = _name;
            int sc = _cname.LastIndexOf('_');
            if (sc > 0)
            {
                _cname = _cname.Substring(0, sc);
                _ctl.Name = _cname;
            }
        }

        public static void ImageToIcon(this LayeredPanel _ctl, AlignType _align, Point _offset, string imgName = "")
        {
            var _name = imgName == String.Empty ? _ctl.Name : imgName;

            var _icon = _ctl.DUIControls.OfType<DuiPictureBox>().FirstOrDefault();

            if (_icon == null)
            {
                _icon = new DuiPictureBox();
                _ctl.DUIControls.Add(_icon);
            }

            if (_icon != null)
            {
                var tmpImg = TMUI.SkinImage(_name);

                if (tmpImg != null)
                {
                    _icon.Size = tmpImg.Size;
                    _icon.BackgroundImage = tmpImg;
                    _icon.AlignControl(_align, _offset);
                }
            }

            var _cname = _name;
            int sc = _cname.LastIndexOf('_');
            if (sc > 0)
            {
                _cname = _cname.Substring(0, sc);
                _ctl.Name = _cname;
            }
        }

        public static void ImageToIcon(this LayeredButton _ctl, string imgName = "")
        {
            var _name = imgName == String.Empty ? _ctl.Name : imgName;

            var _icon = _ctl.DUIControls.OfType<DuiPictureBox>().FirstOrDefault();

            if (_icon == null)
            {
                _icon = new DuiPictureBox();
                _ctl.DUIControls.Add(_icon);
            }

            if (_icon != null)
            {
                var tmpImg = TMUI.SkinImage(_name);

                if (tmpImg != null)
                {
                    _icon.Size = tmpImg.Size;
                    _icon.BackgroundImage = tmpImg;
                    _icon.Location = new Point((_ctl.Width - _icon.Width) / 2, (_ctl.Height - _icon.Height) / 2);
                    _icon.Cursor = _ctl.Cursor;
                }
            }

            var _cname = _name;
            int sc = _cname.LastIndexOf('_');
            if (sc > 0)
            {
                _cname = _cname.Substring(0, sc);
                _ctl.Name = _cname;
            }
        }

        public static void ImageToIcon(this LayeredLabel _ctl, AlignType _align, Point _offset, string imgName = "")
        {
            var _name = imgName == String.Empty ? _ctl.Name : imgName;

            var _icon = _ctl.DUIControls.OfType<DuiPictureBox>().FirstOrDefault();

            if (_icon == null)
            {
                _icon = new DuiPictureBox();
                _ctl.DUIControls.Add(_icon);
            }

            if (_icon != null)
            {
                var tmpImg = TMUI.SkinImage(_name);

                if (tmpImg != null)
                {
                    _icon.Size = tmpImg.Size;
                    _icon.BackgroundImage = tmpImg;
                    _icon.AlignControl(_align, _offset);
                }
            }

            var _cname = _name;
            int sc = _cname.LastIndexOf('_');
            if (sc > 0)
            {
                _cname = _cname.Substring(0, sc);
                _ctl.Name = _cname;
            }
        }

        //AddIcon
        public static void AddIcon(this LayeredLabel _ctl, AlignType _align, Point _offset, string imgName)
        {
            var _name = imgName;
            var _icon = new DuiPictureBox();
            _ctl.DUIControls.Add(_icon);

            if (_icon != null)
            {
                var tmpImg = TMUI.SkinImage(_name);

                if (tmpImg != null)
                {
                    _icon.Size = tmpImg.Size;
                    _icon.BackgroundImage = tmpImg;
                    _icon.AlignControl(_align, _offset);
                }
            }
        }

        public static void AddIcon(this LayeredPanel _ctl, AlignType _align, Point _offset, string imgName)
        {
            var _name = imgName;
            var _icon = new DuiPictureBox();
            _ctl.DUIControls.Add(_icon);

            if (_icon != null)
            {
                var tmpImg = TMUI.SkinImage(_name);

                if (tmpImg != null)
                {
                    _icon.Size = tmpImg.Size;
                    _icon.BackgroundImage = tmpImg;
                    _icon.AlignControl(_align, _offset);
                }
            }
        }

        public static void AddIcon(this LayeredButton _ctl, AlignType _align, Point _offset, string imgName)
        {
            var _name = imgName;
            var _icon = new DuiPictureBox();
            _ctl.DUIControls.Add(_icon);

            if (_icon != null)
            {
                var tmpImg = TMUI.SkinImage(_name);

                if (tmpImg != null)
                {
                    _icon.Cursor = _ctl.Cursor;
                    _icon.Size = tmpImg.Size;
                    _icon.BackgroundImage = tmpImg;
                    _icon.AlignControl(_align, _offset);
                }
            }
        }
        //-------------------------------------------------

        //AddText

        //--------------------------------


        public static void ImageToNormalImage(this LayeredButton _ctl, string imgName = "")
        {
            var _name = imgName == String.Empty ? _ctl.Name : imgName;

            var tmpImg = TMUI.SkinImage(_name);
            if (tmpImg != null)
            {
                _ctl.NormalImage = tmpImg;
            }
        }

        public static void ImageToPressImage(this LayeredButton _ctl, string imgName = "")
        {
            var _name = imgName == String.Empty ? _ctl.Name : imgName;

            var tmpImg = TMUI.SkinImage(_name);
            if (tmpImg != null)
            {
                _ctl.PressedImage = tmpImg;
            }
        }

        public static void ImageToHoverImage(this LayeredButton _ctl, string imgName = "")
        {
            var _name = imgName == String.Empty ? _ctl.Name : imgName;

            var tmpImg = TMUI.SkinImage(_name);
            if (tmpImg != null)
            {
                _ctl.HoverImage = tmpImg;
            }
        }

        public static void ImageToButtonImage(this LayeredButton _ctl, string imgNames = "")
        {
            var _name = imgNames == String.Empty ? _ctl.Name : imgNames;

            _ctl.ImageToNormalImage(_name + "_n");
            _ctl.ImageToPressImage(_name + "_c");
            _ctl.ImageToHoverImage(_name + "_h");

            _ctl.Name = _name;
        }

        //Check Box
        //LayeredCheckButton
        public static void ImageToCheckBoxCheckedNormalImage(this LayeredCheckButton _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.CheckedNormal = tmpImg;
        }

        public static void ImageToCheckBoxCheckedHoverImage(this LayeredCheckButton _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.CheckedHover = tmpImg;
        }

        public static void ImageToCheckBoxCheckedPressImage(this LayeredCheckButton _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.CheckedPressed = tmpImg;
        }

        public static void ImageToCheckBoxUnCheckedNormalImage(this LayeredCheckButton _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.UncheckedNormal = tmpImg;
        }

        public static void ImageToCheckBoxUnCheckedHoverImage(this LayeredCheckButton _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.UncheckedHover = tmpImg;
        }

        public static void ImageToCheckBoxUnCheckedPressImage(this LayeredCheckButton _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.UncheckedPressed = tmpImg;
        }

        public static void ImageToCheckBox(this LayeredCheckButton _lcb, string imgNames = "")
        {
            var _name = imgNames == String.Empty ? _lcb.Name : imgNames;

            _lcb.ImageToCheckBoxCheckedNormalImage(_name + "_on");
            _lcb.ImageToCheckBoxCheckedHoverImage(_name + "_on_h");
            _lcb.ImageToCheckBoxCheckedPressImage(_name + "_on_c");
            _lcb.ImageToCheckBoxUnCheckedNormalImage(_name + "_off");
            _lcb.ImageToCheckBoxUnCheckedHoverImage(_name + "_off_h");
            _lcb.ImageToCheckBoxUnCheckedPressImage(_name + "_off_c");

            _lcb.Name = _name;

            foreach (DuiBaseControl item in _lcb.DUIControls)
            {
                item.Cursor = _lcb.Cursor;
            }
        }
        //DuiCheckBox
        public static void ImageToCheckBoxCheckedNormalImage(this DuiCheckBox _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.CheckedNormal = tmpImg;
        }

        public static void ImageToCheckBoxCheckedHoverImage(this DuiCheckBox _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.CheckedHover = tmpImg;
        }

        public static void ImageToCheckBoxCheckedPressImage(this DuiCheckBox _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.CheckedPressed = tmpImg;
        }

        public static void ImageToCheckBoxUnCheckedNormalImage(this DuiCheckBox _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.UncheckedNormal = tmpImg;
        }

        public static void ImageToCheckBoxUnCheckedHoverImage(this DuiCheckBox _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.UncheckedHover = tmpImg;
        }

        public static void ImageToCheckBoxUnCheckedPressImage(this DuiCheckBox _lcb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);

            _lcb.UncheckedPressed = tmpImg;
        }

        public static void ImageToCheckBox(this DuiCheckBox _lcb, string imgNames = "")
        {
            var _name = imgNames == String.Empty ? _lcb.Name : imgNames;

            _lcb.ImageToCheckBoxCheckedNormalImage(_name + "_on");
            _lcb.ImageToCheckBoxCheckedHoverImage(_name + "_on_h");
            _lcb.ImageToCheckBoxCheckedPressImage(_name + "_on_c");
            _lcb.ImageToCheckBoxUnCheckedNormalImage(_name + "_off");
            _lcb.ImageToCheckBoxUnCheckedHoverImage(_name + "_off_h");
            _lcb.ImageToCheckBoxUnCheckedPressImage(_name + "_off_c");

            _lcb.Name = _name;

            foreach (DuiBaseControl item in _lcb.Controls)
            {
                item.Cursor = _lcb.Cursor;
            }
        }

        public static void ImageToIcon(this LayeredCheckButton _ctl, string imgName = "")
        {
            var _name = imgName == String.Empty ? _ctl.Name : imgName;

            var _icon = _ctl.DUIControls.OfType<DuiPictureBox>().FirstOrDefault();

            if (_icon == null)
            {
                _icon = new DuiPictureBox();
                _ctl.DUIControls.Add(_icon);
            }

            if (_icon != null)
            {
                var tmpImg = TMUI.SkinImage(_name);

                if (tmpImg != null)
                {
                    _icon.Size = tmpImg.Size;
                    _icon.BackgroundImage = tmpImg;
                    _icon.Location = new Point((_ctl.Width - _icon.Width) / 2, (_ctl.Height - _icon.Height) / 2);
                    _icon.Cursor = _ctl.Cursor;
                }
            }

            var _cname = _name;
            int sc = _cname.LastIndexOf('_');
            if (sc > 0)
            {
                _cname = _cname.Substring(0, sc);
                _ctl.Name = _cname;
            }
        }

        //Track bar

        public static void UpdateLayout(this LayeredCheckButton _ltb)
        {
            var tmp = _ltb.Checked;
            _ltb.Checked = !tmp;
            _ltb.Checked = tmp;
        }

        public static void ImageToTrackbarBackImage(this LayeredTrackBar _ltb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);
            _ltb.BackImage = tmpImg;
        }

        public static void ImageToTrackbarPointImage(this LayeredTrackBar _ltb, string imgName = "")
        {
            _ltb.PointImage = TMUI.SkinImage(imgName + "_n");
            _ltb.PointImageHover = TMUI.SkinImage(imgName + "_h");
            _ltb.PointImagePressed = TMUI.SkinImage(imgName + "_c");
        }

        public static void ImageToTrackbarSurfaceImage(this LayeredTrackBar _ltb, string imgName = "")
        {
            var tmpImg = TMUI.SkinImage(imgName);
            _ltb.SurfaceImage = tmpImg;
        }

        public static void ImageToTrackbar(this LayeredTrackBar _ltb, Point _offset, string bg = "", string pts = "", string sf = "")
        {
            _ltb.ImageToTrackbarBackImage(bg);
            _ltb.ImageToTrackbarPointImage(pts);
            _ltb.ImageToTrackbarSurfaceImage(sf);

            if (_ltb.SurfaceImage != null & _ltb.BackImage != null)
            {
                float _width = _ltb.BackImage.Width;
                float _height = _ltb.BackImage.Height;
                float _swidth = _ltb.SurfaceImage.Width;
                float _sheight = _ltb.SurfaceImage.Height;
                int x = (int)((_width - _swidth) / 2 + _offset.X);
                int y = (int)((_height - _sheight) / 2 + _offset.Y);
                int w = (int)(_swidth);
                int h = (int)(_sheight);

                Rectangle tmpRectangle = new Rectangle(x, y, w, h);
                _ltb.ControlRectangle = tmpRectangle;
            }

            _ltb.Name = bg;
        }


        public static void ImageToBackground(this Control ctl, Image img)
        {
            if (img != null)
            {
                ctl.BackgroundImage = img;
            }
        }

        public static void ImageToBackground(Image img, params Control[] ctls)
        {
            for (int i = 0; i < ctls.Length; i++)
            {
                ctls[i].ImageToBackground(img);
            }
        }

        public static IOrderedEnumerable<IGrouping<string, string>> GroupItems(string[] itemNames)
        {
            IOrderedEnumerable<IGrouping<string, string>> itemGP = itemNames.GroupBy(user => user.Substring(0, 1)).OrderBy(gp => gp.Key);
            return itemGP;
        }

        public static IOrderedEnumerable<IGrouping<string, string>> GroupItems(List<string> itemNames)
        {
            IOrderedEnumerable<IGrouping<string, string>> itemGP = itemNames.GroupBy(user => user.Substring(0, 1)).OrderBy(gp => gp.Key);
            return itemGP;
        }

        #endregion


        #region Layout

        public static void SetBorders(this DuiBaseControl _dbc, params object[] args)
        {
            _dbc.Borders.LeftColor = (Color)args[0];
            _dbc.Borders.TopColor = (Color)args[1];
            _dbc.Borders.RightColor = (Color)args[2];
            _dbc.Borders.BottomColor = (Color)args[3];

            _dbc.Borders.LeftWidth = (int)args[4];
            _dbc.Borders.TopWidth = (int)args[5];
            _dbc.Borders.RightWidth = (int)args[6];
            _dbc.Borders.BottomWidth = (int)args[7];
        }

        public static void SetBackground(this Control _ctl, Color _bgColor)
        {
            _ctl.BackColor = _bgColor;
        }

        public static void SetBackground(this Control _ctl, Image _bgImage)
        {
            _ctl.BackgroundImage = _bgImage;
        }

        #endregion


        #region TOOL

        /// <summary>
        /// 检查呼叫号码格式是否正确
        /// </summary>
        /// <param name="callID"></param>
        /// <returns></returns>
        public static int CheckCallID(string callID)
        {
            int err = 0;

            callID = callID.Trim();

            if (callID == "")
            {
                err = 1;
            }
            else if (callID == Configuration.Account)
            {
                err = 2;
            }

            //var m = Regex.Match(callID, @"[^0-9]+");
            //if (m.Success)
            //{
            //    status = "账号格式不正确，请重新输入";
            //    labMemberState.Text = status;
            //    textCallerId.Focus();
            //    isOK = false;
            //}

            return err;
        }

        public static int IntPtrToInt(IntPtr _intPtr)
        {
            return _intPtr.ToInt32();
        }

        #endregion

    }
}
